package com.jp.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            /**
             * @Todo load.jpg放在assets中不压缩  4是默认以ARGB_8888加载的
             */
            Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("load.jpg"));
            int allocationByteCount = bitmap.getAllocationByteCount();//Bitmap.getAllocationByteCount() 方法获取 Bitmap 占用的字节大小
            Log.d(TAG, "onCreate: allocationByteCount = " + allocationByteCount);//1024*5825*4 = 23859200

            Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.load);
            int allocationByteCount1 = bitmap1.getAllocationByteCount();
            /**
             * @Todo 图片放不同的文件夹，内存占用的不同
             * 目标手机屏幕密度是480 加载的图片是存放早hdpi中240的屏幕密度 scale = 2
             * 缩放比例 scale = 当前设备屏幕密度 / 图片所在 drawable 目录对应屏幕密度
             * Bitmap 实际大小 = 宽 * scale * 高 * scale * Config 对应存储像素数 1024*2*5825*2 *4 = 95436800
             */

            Log.d(TAG, "onCreate: allocationByteCount1 = " + allocationByteCount1);


            //@Todo 设置颜色配置RGB_565  对应加载一个像素两个字节
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.mipmap.load, options);
            int allocationByteCount2 = bitmap2.getAllocationByteCount();//对应存储像素数 1024*2*5825*2 *2 = 47718400
            Log.d(TAG, "onCreate: allocationByteCount2 = " + allocationByteCount2);


            //@Todo 另外 Options 中还有一个 inSampleSize 参数，可以实现 Bitmap 采样压缩，这个参数的含义是宽高维度上每隔 inSampleSize 个像素进行一次采集

            options.inSampleSize = 2;//宽、高都缩小2倍 整体缩小4倍

            Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(), R.mipmap.load, options);
            int allocationByteCount3 = bitmap3.getAllocationByteCount();//对应存储像素数 1024*2/2 *5825*2/2 *2 = 11931648
            Log.d(TAG, "onCreate: allocationByteCount3 = " + allocationByteCount3);

            Log.d(TAG, "onCreate:  options.inDensity = " + options.inDensity +
                    "  options.inTargetDensity = " + options.inTargetDensity +
                    "   options.inScaled = " + options.inScaled);
            //不设置options.inDensity和options.inTargetDensity缩放比就是默认的手机屏幕密度/文件夹图片密度  480/240
            //设置了options.inDensity和options.inTargetDensity scale = options.inTargetDensity/options.inDensity 这样就和图片放在哪个文件夹没得关系的
            options.inDensity = options.outWidth;
            options.inTargetDensity = 1080;//scale = 1080/1024 = 1.055
            //options.inScaled = false;不允许缩放
            Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(), R.mipmap.load, options);
            int allocationByteCount4 = bitmap4.getAllocationByteCount();//1024*1.055*5825*1.055*2
            Log.d(TAG, "onCreate: allocationByteCount4 = " + allocationByteCount4);


        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}